<script>
import Vue from 'vue';
Vue.filter('encrypt', function(val, type) {

	const REG_MOBILE = /^\d{11}$/;
	const EMAIL_MOBILE = /^[a-zA-Z0-9_\.\-]+@[a-zA-Z0-9\-]+\.(?:[a-zA-Z0-9]{2,4})$/;
	const ID_CARD_15 = /^[1-9]\d{7}(?:(?:0\d)|(?:1[0-2]))(?:(?:[0|1|2]\d)|3[0-1])\d{3}$/;
	const ID_CARD_18 = /^[1-9]\d{5}[1-9]\d{3}(?:(?:0\d)|(?:1[0-2]))(?:(?:[0|1|2]\d)|3[0-1])\d{3}[0-9xX]$/; // eslint-disable-line max-len

	if(!val) {
		return '';
	}
console.log(type)
	const value = val.toString();
	if(type === 'username') {
		if(value.length === 2) {
			return `${value.charAt(0)}*`;
		}
		return `${value.charAt(0)}${(new Array(value.length - 1)).
    join('*')}${value.charAt(value.length - 1)}`;
	}
	if(type === 'mobile' && REG_MOBILE.test(value)) { // 手机号
		return value.replace(/(\d{3})(\d{4})(\d{4})/, '$1****$3');
	}

	if(type === 'email' && EMAIL_MOBILE.test(value)) { // 邮箱
		return value.replace(/^([a-zA-Z0-9_\.\-])([a-zA-Z0-9_\.\-]+)([a-zA-Z0-9_\.\-])(@[a-zA-Z0-9\-]+\.(?:[a-zA-Z0-9]{2,4}))$/, '$1****$3$4'); // eslint-disable-line max-len
	}

	if(ID_CARD_15.test(value) || ID_CARD_18.test(value)) { // 身份证
		return value.replace(/^(.{2})(.+)(.{2})$/, '$1****$3');
	}
	const rv = value.replace(/^\s+|\s+$/g, '');
	return `${rv.charAt(0) + rv.substr(1).replace(/./g, '*')}`;

});
</script>